#include"nms.h"

float iou(Bboxcv box1, Bboxcv box2)
{
	int area_1 = box1.rect.area();
	int area_2 = box2.rect.area();
	cv::Rect intersection = box1.rect | box2.rect;
	cv::Rect union_area = box1.rect & box2.rect;
	return  union_area.area()*1.0 /intersection.area();
}

bool comparison(Bboxcv a, Bboxcv b) {
    return a.score > b.score;
}

int nms(std::vector<Bboxcv>& result_obj, std::vector<Bboxcv>& result_obj_nms, float nms_thres)
{
    sort(result_obj.begin(), result_obj.end(), comparison);
    for (int i = 0; i < result_obj.size(); i++)
    {
        if (result_obj[i].status)
        {
            result_obj_nms.push_back(result_obj[i]);
        }
        for (int j = i; j < result_obj.size(); j++)
        {
            if (result_obj[i].label == result_obj[j].label && iou(result_obj[i], result_obj[j]) > nms_thres)
            {
                result_obj[j].status = false;
            }
        }
    }
    return 0;
}
